package com.yuke.cloud.service.cmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.opc.service.OpcSendNotifyFeignApi;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.cmc.dao.CmsNoticeMapper;
import com.yuke.cloud.service.cmc.entity.CmsNotice;
import com.yuke.cloud.service.cmc.service.CmsNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by wg on 2019/09/27.
 */
@Service
@Transactional
public class CmsNoticeServiceImpl extends BaseService<CmsNotice> implements CmsNoticeService {
    @Resource
    private CmsNoticeMapper cmsNoticeMapper;

    @Resource
    private OpcSendNotifyFeignApi opcSendNotifyFeignApi;

    @Resource
    private SmcFeignApi smcFeignApi;

    //通知信息
    @Override
    public List<CmsNotice> getInformation(CmsNotice cmsNotice) {

        List<CmsNotice> informationList = cmsNoticeMapper.getInformation(cmsNotice);
        List<ShopInfoDto> shops = smcFeignApi.getShopIdsByCondition();
        System.out.println(shops);
        //消息发送给所有商家
        //getLoginAuthDto().getUserId()
        if (informationList != null && informationList.size() > 0) {
            for (CmsNotice notice : informationList) {
                if (notice.getnStatus() == 0) {
                    String notificationTitle = notice.getnTitle();
                    String subtitle = notice.getnSubTitle();
                    if (shops != null && shops.size() > 0) {
                        for (ShopInfoDto shop : shops) {
                            System.out.println(shop.getUserId().toString());
                            opcSendNotifyFeignApi.sendnotify(shop.getUserId().toString(), notificationTitle, subtitle, 0);
                        }
                    }
                }
            }
        }
        return informationList;
    }

    /**
     * 发布活动通知消息
     *
     * @param cmsNotice
     * @return
     */
    @Override
    public CmsNotice modifyInformation(CmsNotice cmsNotice) {
        List<ShopInfoDto> shops = smcFeignApi.getShopIdsByCondition();
        cmsNotice.setnStatus((byte) (cmsNotice.getnStatus() + 1));
        cmsNoticeMapper.updateByPrimaryKey(cmsNotice);
        if (cmsNotice.getnStatus() < 3) {
            String notificationTitle = cmsNotice.getnTitle();
            String subtitle = cmsNotice.getnSubTitle();
            if (shops != null && shops.size() > 0) {
                for (ShopInfoDto shop : shops) {
                    opcSendNotifyFeignApi.sendnotify(shop.getUserId().toString(), notificationTitle, subtitle, 0);
                }
            }
        }
        return cmsNotice;
    }

    @Override
    public CmsNotice getNewNotice() {

        return cmsNoticeMapper.getNewNotice();
    }
}
